.TH std::bernoulli_distribution 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::bernoulli_distribution \- std::bernoulli_distribution

.SH Synopsis
   Defined in header <random>
   class bernoulli_distribution;  \fI(since C++11)\fP

   Produces random boolean values, according to the discrete probability function. The
   probability of true is

   P(b|p) =⎧
   ⎨
                                                                       ⎩p, if b is true
   1 − p, if b is false

   std::bernoulli_distribution satisfies RandomNumberDistribution.

.SH Member types

   Member type         Definition
   result_type \fI(C++11)\fP bool
   param_type \fI(C++11)\fP  the type of the parameter set, see RandomNumberDistribution.

.SH Member functions

   constructor   constructs new distribution
   \fI(C++11)\fP       \fI(public member function)\fP
   reset         resets the internal state of the distribution
   \fI(C++11)\fP       \fI(public member function)\fP
.SH Generation
   operator()    generates the next random number in the distribution
   \fI(C++11)\fP       \fI(public member function)\fP
.SH Characteristics
   p             returns the p distribution parameter (probability of generating true)
   \fI(C++11)\fP       \fI(public member function)\fP
   param         gets or sets the distribution parameter object
   \fI(C++11)\fP       \fI(public member function)\fP
   min           returns the minimum potentially generated value
   \fI(C++11)\fP       \fI(public member function)\fP
   max           returns the maximum potentially generated value
   \fI(C++11)\fP       \fI(public member function)\fP

.SH Non-member functions

   operator==
   operator!=                compares two distribution objects
   \fI(C++11)\fP                   \fI(function)\fP
   \fI(C++11)\fP(removed in C++20)
   operator<<                performs stream input and output on pseudo-random number
   operator>>                distribution
   \fI(C++11)\fP                   \fI(function template)\fP

.SH Example


// Run this code

 #include <iomanip>
 #include <iostream>
 #include <map>
 #include <random>
 #include <string>

 int main()
 {
     std::random_device rd;
     std::mt19937 gen(rd());
     // give "true" 1/4 of the time
     // give "false" 3/4 of the time
     std::bernoulli_distribution d(0.25);

     std::map<bool, int> hist;
     for (int n = 0; n < 10000; ++n)
         ++hist[d(gen)];

     std::cout << std::boolalpha;
     for (auto const& [key, value] : hist)
         std::cout << std::setw(5) << key << ' '
                   << std::string(value / 500, '*') << '\\n';
 }

.SH Possible output:

 false ***************
  true ****

.SH External links

   Weisstein, Eric W. "Bernoulli Distribution." From MathWorld — A Wolfram Web
   Resource.
